// pages/brandmenu/brandmenu.js
import {getTertiaryDirectory} from '../../api/Category'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        flag:false,
        name:'',
        goodsList: [
        //     {
        //     id: '',
        //     goodsImage: "",
        //     goodsName: "",
        //     goodsAddress: "",
        //     goodsPrice: ""
        // }
        // {
        //     id: 2,
        //     goodsImage: "/images/goods01.jpg",
        //     goodsName: "商品名称2",
        //     goodsAddress: "广州",
        //     goodsPrice: "200"
        // }
    ]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        // console.log("页面显示");
       this.setData({
            name : wx.getStorageSync('name')
       });
    //    console.log(this.data.name);
        if(wx.getStorageSync('query')){
            let query = wx.getStorageSync('query');
            getTertiaryDirectory({query:query}).then(data=>{
                console.log(data);
                let lists =[];
                data.message.goods.forEach(item=>{
                    let {goods_id,goods_name,goods_price,goods_big_logo} = item;
                    let list ={
                        id:goods_id,
                        goodsImage:goods_big_logo,
                        goodsName:goods_name,
                        goodsAddress:"广州",
                        goodsPrice:goods_price
                    }
                    if(goods_big_logo){
                        lists.push(list);
                    }
                    this.setData({
                        goodsList:lists
                    })
                })
            })  
        }else{
            let cid = wx.getStorageSync('cid');
            getTertiaryDirectory({cid:cid}).then(data=>{
                console.log(data);
                if(data.message.goods.length == 0){
                    this.setData({
                        flag : true
                    })
                }
                let lists =[];
                data.message.goods.forEach(item=>{
                    let {goods_id,goods_name,goods_price,goods_big_logo} = item;
                    let list ={
                        id:goods_id,
                        goodsImage:goods_big_logo,
                        goodsName:goods_name,
                        goodsAddress:"广州",
                        goodsPrice:goods_price
                    }
                    if(goods_big_logo){
                        lists.push(list);
                    }
                    this.setData({
                        goodsList:lists
                    })
                    console.log(this.data.goodsList);
                })
            })  
        }
       
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        wx.removeStorageSync('name');
        wx.removeStorageSync('query')
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})